const db = wx.cloud.database({
    env: 'work1-0g1b3grp436e9236'
})
const dayjs = require('../../miniprogram_npm/miniprogram_npm/dayjs/index')
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'

Page({
    options: {
        styleIsolation: 'shared'
    },
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            active: 'activeTabBarIndex'
        },
        actions: {
            updateActive: 'updateActiveTabBarIndex'
        },
    },

    /**
     * 页面的初始数据
     */
    data: {
        keyword: '募农帮，助力乡村振兴！',
        activeTab: 0,
        jobList: [],
        curPage: 0,
        loading: true,
        showOverlay: true
    },

    toLoginPage() {
        wx.switchTab({
          url: '/pages/center/center',
        })
        this.setData({showOverlay: false})
    },

    onClickShowOverlay() {
        this.setData({ showOverlay: true })
    },

    onClickHideOverlay() {
        this.setData({ showOverlay: false })
    },

    // 推荐：按日工资排序
    async getJobList(orderBy = 'salary', order = 'desc', limit = 10) {
        let res = await db.collection('work_job').skip(this.data.curPage * limit).limit(limit)
            .where({
                status: 1
            }).orderBy(orderBy, order).get()
        let jobList = res.data
        for (let i = 0; i < jobList.length; i++) {
            const job = jobList[i]
            jobList[i].endTime = dayjs(job.endTime).format("YYYY-MM-DD")
            const res = await db.collection('work_user').doc(job.userId).get()
            jobList[i].user = res.data
        }
        this.setData({
            jobList: this.data.jobList.concat(jobList),
            loading: false
        })
        console.log(jobList)
    },

    toSearchPage() {
        wx.navigateTo({
            url: '/pages/search/search'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getJobList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.updateActive(0)
        } 
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
        wx.showNavigationBarLoading()
        this.setData({
            jobList: [],
            curPage: 0,
            loading: true
        })
        await this.getJobList()
        wx.hideNavigationBarLoading()
        // 数据成功后，停止下拉刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        wx.showLoading({
            title: '加载中..',
        })
        this.setData({
            curPage: this.data.curPage + 1
        })
        this.getJobList().then(() => {
            // 数据成功后，停止下拉刷新
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})